﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using CDSB.Indicators.Model;

namespace CDSB.Indicators.Comum
{
    public static class CommonFunctions
    {
        /// <summary>
        /// A partir de um date time, retorna a data no formato usado nas wiql do Team System.
        /// </summary>
        /// <param name="date">Data a ser formatada</param>
        /// <returns>string</returns>
        public static string ToTeamSystemDateFormat(this DateTime date)
        {
            string newDate = string.Empty;

            // Ano-Mês-DiaTHora:Min:Seg.Mil
            //newDate = "{0}-{1}-{2}T{3}:{4}:{5}.{6}";
            newDate = "{0}-{1}-{2}";

            //string Hour = date.Hour > 0 ? date.Hour.ToString() : "00";
            //string Min = date.Minute > 0 ? date.Minute.ToString() : "00";
            //string Sec = date.Second > 0 ? date.Second.ToString() : "00";
            //string Mil = date.Millisecond > 0 ? date.Millisecond.ToString().PadLeft(7,'0') : "0000000";

            //newDate = string.Format(newDate, date.Year, date.Month, date.Day, Hour, Min, Sec, Mil);
            newDate = string.Format(newDate, date.Year, date.Month, date.Day);

            return newDate;
        }

        public static CDSBScenario FindScenario(this IEnumerable<CDSBScenario> scenario, int idScenario)
        {
            return scenario.ToList<CDSBScenario>().Find(delegate(CDSBScenario delegateScenario) { return delegateScenario.ID == idScenario; });
        }

        public static CDSBProject FindProject(this List<CDSBProject> ProjectList, string areaPath)
        {
            return ProjectList.Find(delegate(CDSBProject delegateProject) { return delegateProject.AreaPathList.Contains(areaPath); });
        }

        public static CDSBFactory FindFactory(this List<CDSBFactory> FactoryList, string factoryName)
        {
            return FactoryList.Find(delegate(CDSBFactory delegateFactory) { return delegateFactory.Factory == factoryName; });
        }

        public static Int32 GetTotalWorkItems(this List<CDSBScenario> scenarioCollection)
        {
            Int32 WorkItemCount = 0;
            
            scenarioCollection.ForEach((item) => 
            {
                WorkItemCount += item.BugsList.Count + item.TasksList.Count(delegate(CDSBTaskDesenv task) { return task.IsOldTask == false; });
            });

            return WorkItemCount;
        }
        
        public static Int32 GetTotalOfBugs(this List<CDSBScenario> scenarioCollection) 
        {
            Int32 BugsCount = 0;

            scenarioCollection.ForEach((item) =>
            {
                BugsCount += item.BugsList.Count;
            });

            return BugsCount;
        }

        public static Int32 GetTotalTasks(this List<CDSBScenario> scenarioCollection)
        {
            Int32 TaskCount = 0;

            scenarioCollection.ForEach((item) =>
            {
                TaskCount += item.TasksList.Count(delegate(CDSBTaskDesenv task) { return task.IsOldTask == false; });
            });

            return TaskCount;
        }

        public static Int32 GetTotalOutOfDateTasks(this List<CDSBScenario> scenarioCollection) 
        {
            Int32 OutOfDateCount = 0;

            scenarioCollection.ForEach((item) =>
            {
                OutOfDateCount += item.TasksList.Count(delegate(CDSBTaskDesenv task) { return task.RealizedFinishDate.Date > task.EstimatedFinishDate.Date && task.IsOldTask == false; });
            });

            return OutOfDateCount;
        }

        public static Int32 GetTotalCodeReviewNotApproved(this List<CDSBScenario> scenarioCollection)
        {
            Int32 CodeReviewCount = 0;

            scenarioCollection.ForEach((item) =>
            {
                CodeReviewCount += item.TasksList.Count(delegate(CDSBTaskDesenv task) { return task.IsCodeApproved == false && task.IsOldTask == false; });
            });

            return CodeReviewCount;
        }

        public static Int32 GetTotalTasksReviewed(this List<CDSBScenario> scenarioCollection)
        {
            Int32 CodeReviewCount = 0;

            scenarioCollection.ForEach((item) =>
            {
                CodeReviewCount += item.TasksList.Count(delegate(CDSBTaskDesenv task) { return (task.IsCodeApproved != null) && task.IsOldTask == false; });
            });

            return CodeReviewCount;
        }

        public static bool ContainsId(this WorkItemCollection wItemCollection, int id) 
        {
            bool Contains = false; 

            foreach (WorkItem wItem in wItemCollection) 
            {
                if (wItem.Id == id) 
                {
                    Contains = true;
                    break;
                }
            }

            return Contains;
        }
    }
}
